package http

import (
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/dto/reqdata"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/pkg/gin_http"
	v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
	"github.com/gin-gonic/gin"
	"go.uber.org/dig"
)

type SettingController struct {
	l v1log.ILog

	conf *configstc.SettingAppConfig

	settingSvc interfaces.ItfSettingService

	gin_http.ResponseController
}

func NewSettingController(di *dig.Container, l v1log.ILog, conf *configstc.SettingAppConfig, svc interfaces.ItfSettingService) *SettingController {
	ctr := &SettingController{
		l:    l,
		conf: conf,
	}

	ctr.settingSvc = svc

	return ctr
}

// GetSetting 获取配置 godoc
// @Summary      获取单个配置
// @Description  获取单个配置
// @Tags         setting
// @Produce      json
// @Security     AdminKeyAuth
// @Param        param    body     reqdata.QuerySettingReq  true  "参数"
// @success 200  {object} respdata.ResponseData{data=respdata.QuerySettingResp} "获取成功"
// @success 500 {object} respdata.ResponseData{} "获取失败"
// @Router       /setting/get [post]
func (ctr *SettingController) GetSetting(ctx *gin.Context) {
	var req reqdata.QuerySettingReq
	ctx.ShouldBindJSON(&req)
	if req.Key == "" {
		ctr.Response(ctx, respdata.CParamsInvalid)
		return
	}

	value, err := ctr.settingSvc.Get(req.Key)
	if err != nil {
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess.MData(respdata.QuerySettingResp{req.Key: value}))
	return
}

// PutSetting 提交配置 godoc
// @Summary      提交单个配置
// @Description  提交单个配置
// @Tags         setting
// @Produce      json
// @Security     AdminKeyAuth
// @Param        param    body     reqdata.PutSettingReq  true  "参数"
// @success 200  {object} respdata.ResponseData{} "配置成功"
// @success 500 {object} respdata.ResponseData{} "配置失败"
// @Router       /setting/put [post]
func (ctr *SettingController) PutSetting(ctx *gin.Context) {
	var req reqdata.PutSettingReq
	ctx.ShouldBindJSON(&req)

	err := ctr.settingSvc.Set(req.Key, req.Value)
	if err != nil {
		ctr.l.Error("setting save %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}
	ctr.Response(ctx, respdata.CSuccess)
}

// GetMultiSetting 获取配置 godoc
// @Summary      批量获取配置
// @Description  批量获取多个配置
// @Tags         setting-multi
// @Produce      json
// @Security     AdminKeyAuth
// @Param        param    body     reqdata.QueryMultiSettingReq  true  "参数"
// @success 200  {object} respdata.ResponseData{data=respdata.QuerySettingResp} "获取成功"
// @success 500 {object} respdata.ResponseData{} "获取失败"
// @Router       /setting/multi/get [post]
func (ctr *SettingController) GetMultiSetting(ctx *gin.Context) {
	var req reqdata.QueryMultiSettingReq
	ctx.ShouldBindJSON(&req)
	if len(req.Keys) == 0 {
		ctr.Response(ctx, respdata.CParamsInvalid)
		return
	}

	data, err := ctr.settingSvc.GetMulti(req.Keys...)
	if err != nil {
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess.MData(respdata.QuerySettingResp(data)))
	return
}

// PutMultiSetting 提交配置 godoc
// @Summary      批量提交配置
// @Description  批量提交多个配置
// @Tags         setting-multi
// @Produce      json
// @Security     AdminKeyAuth
// @Param        param    body     reqdata.PutMultiSettingReq  true  "参数"
// @success 200  {object} respdata.ResponseData{} "配置成功"
// @success 500 {object} respdata.ResponseData{} "配置失败"
// @Router       /setting/multi/put [post]
func (ctr *SettingController) PutMultiSetting(ctx *gin.Context) {
	var req reqdata.PutMultiSettingReq
	ctx.ShouldBindJSON(&req)

	err := ctr.settingSvc.SetMulti(req.Data)
	if err != nil {
		ctr.l.Error("setting save %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}
	ctr.Response(ctx, respdata.CSuccess)
}
